﻿using System;
using System.Runtime.Serialization;
using CC.Yacht.Interfaces;
using CC.Yacht.Utilities;

namespace CC.Yacht
{
    [DataContract]
    public class Die: IDie
    {
        #region Constructor
        public Die() : this(DieFace.None)
        {
            // Empty constructor
        }

        public Die(DieFace face)
        {
            _Face = face;
        }

        public Die(DieFace face, bool locked)
        {
            _Face = face;
            _Locked = locked;
        }
        #endregion

        #region Private Fields
        private DieFace _Face = DieFace.None;
        private bool _Locked;
        #endregion

        #region Public Events
        public event EventHandler<DieChangedEventArgs> Changed;
        #endregion

        #region Public Properties
        [DataMember]
        public DieFace Face
        {
            get { return _Face; }
            set
            {
                if (_Face != value)
                {
                    _Face = value;

                    OnChanged(new DieChangedEventArgs(value));
                }
            }
        }

        [DataMember]
        public bool Locked
        {
            get { return _Locked; }
            set
            {
                if (_Locked != value)
                {
                    _Locked = value;

                    OnChanged(new DieChangedEventArgs(value));
                }
            }
        }
        #endregion

        #region Protected Methods
        protected void OnChanged(DieChangedEventArgs eventArgs)
        {
            if (Changed != null)
            {
                Changed(this, eventArgs);
            }
        }
        #endregion

        #region Public Methods
        public void Reset()
        {
            _Face = DieFace.None;
            _Locked = false;

            OnChanged(new DieChangedEventArgs(_Face, _Locked));
        }

        public void Roll()
        {
            Face = RandomHelper.GetDieFace();
        }
        #endregion
    }
}
